$(function(){
	var changeTime = function(time) {
		var date = new Date(time);
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		var day = date.getDate();
		var hour = date.getHours();
		var min = date.getMinutes();

		return year+'-'+month+'-'+day+' '+hour+':'+min;
	}

	var orderDetail = {
		getData: function() {
			$.ajax({
			    url: wsCommon.host + '/api/orders/queryByOrderId',
			    dataType: wsCommon.jsonType,
				data: {
					orderId: wsCommon.getUrlParam('orderId')
				},
			    success: function(res) {
					if(res.code == 1) {
						if(res.result) {
							var orders = res.data.orders;
							var orderPmtList = (typeof(res.data.orderPmtList) != 'undefined') ? res.data.orderPmtList : [];
							var orderLogList = (typeof(res.data.orderLogList) != 'undefined') ? res.data.orderLogList : [];

							$('[data-role="orderid"]').html(orders.orderId);
							$('[data-role="ordermoney"]').html('¥'+orders.finalAmount);
							$('[data-role="orderstatus"]').html(orders.statusDesc+' ');
							$('[data-role="orderintro"]').html('说明：'+orders.orderDesc + '<a href="javascript:void(0);" class="action-payment-guide">支付说明</a>');

							if(orders.payStatus == 0 && orders.tatus == 'active') {
								$('[data-role="toorder"]').show();
								$(document).on('click', '[data-role="toorder"]', function() {
									var orderInfo = {
	                                    orderId: orders.orderId
	                                };

	                                wsCommon.setCookie('orderInfo', JSON.stringify(orderInfo));

	                                window.location.href = './cart3.html';
								})
							}

							//流程
							var stepTpl = '';
							if(orders.status == 'active') {
								var step2Class = (orders.payStatus == 1) ? 'active' : '';
								var step3Class = (orders.shipStatus == 1) ? 'active' : '';
								stepTpl += '<td class="step-item step-1 active"><span><q class="icon">P</q><i>1</i>提交订单</span></td>\
												<td class="icon step-arrow">(</td>\
												<td class="step-item step-2 '+step2Class+'"><span><q class="icon">P</q><i>2</i>付款成功</span></td>\
												<td class="icon step-arrow">(</td>\
												<td class="step-item step-3 '+step3Class+'"><span><q class="icon">P</q><i>3</i>发货</span></td>\
												<td class="icon step-arrow">(</td>\
												<td class="step-item step-4 "><span><q class="icon">P</q><i>4</i>订单完成</span></td>';
							} else if(orders.status == 'dead') {
								stepTpl += '<td class="step-item step-1 active"><span><q class="icon">P</q><i>1</i>提交订单</span></td>\
												<td class="icon step-arrow">(</td>\
												<td class="step-item step-2 active"><span><q class="icon">P</q><i>2</i>订单作废</span></td>';
							} else if(orders.status == 'finish') {
								stepTpl += '<td class="step-item step-1 active"><span><q class="icon">P</q><i>1</i>提交订单</span></td>\
												<td class="icon step-arrow">(</td>\
												<td class="step-item step-2 active"><span><q class="icon">P</q><i>2</i>付款成功</span></td>\
												<td class="icon step-arrow">(</td>\
												<td class="step-item step-3  active"><span><q class="icon">P</q><i>3</i>发货</span></td>\
												<td class="icon step-arrow">(</td>\
												<td class="step-item step-4  active"><span><q class="icon">P</q><i>4</i>订单完成</span></td>';
							}
							$('[data-role="step"]').html(stepTpl);

							// 订单信息流程
							var infoStepTpl = '';
							$.each(orderLogList, function(index, item) {
								if(typeof(item.logDetail) != 'undefined' && item.logDetail != null && item.logDetail.length > 0) {
									infoStepTpl += '<li><em>'+changeTime(item.altTime)+'</em>'+item.logText + '<a href="javascript:void(0)" title="点击查看详细" style="color: rgb(0, 51, 102); font-weight: bolder; text-decoration: underline;" class="showdetail">查看详情</a><ul class="notice-inline" style="display: none;">';
									$.each(item.logDetail, function(indexDir, itemDir) {
										infoStepTpl += '<li><em>商品名称：'+itemDir.name+'</em><i>数量：'+itemDir.count+'</i></li>';
									})
									infoStepTpl += '</ul></li>'
								} else {
									infoStepTpl += '<li><em>'+changeTime(item.altTime)+'</em>'+item.logText+'</li>';
								}
							})
							$('.member-order-flow ul').html(infoStepTpl);


							// 收货地址信息
							$('[data-addr="area"]').html(orders.shipArea);
							$('[data-addr="shipAddr"]').html(orders.shipAddr);
							$('[data-addr="name"]').html(orders.shipName);
							$('[data-addr="tel"]').html(orders.shipMobile);
							$('[data-addr="time"]').html(orders.shipTime);

							// 配送方式
							$('[data-dev="style"]').html(orders.shipping);
							$('[data-dev="money"]').html('¥'+orders.costFreight);

							//支付方式
							$('[data-pay="method"]').html(orders.paymentName);

							// 发票
							if(orders.isTax == 'false') {
								$('[data-role="tax"]').hide();
							} else {
								$('[data-tax="style"]').html(orders.taxType == "company" ? '公司发票' : orders.taxType);
								$('[data-tax="taitou"]').html(orders.taxCompany);
								$('[data-tax="content"]').html(orders.taxContent);
							}

							// 商品
							var itemsList = orders.itemsList;
							var itemsListTpl = '';
							$.each(itemsList, function(index, item) {
								itemsListTpl += '<tr class="cart-product">\
													<td class="p-info">\
														<div class="p-title">\
															<a href="./info.html?goodsId='+item.goodsId+'" target="_blank">'+item.name+'</a>\
														</div>\
													</td>\
													<td>'+item.nums+'</td>\
												</tr>';
							})
							$('[data-role="goods"]').html(itemsListTpl);

							//订单优惠
							if(orderPmtList.length == 0) {
								$('[data-role="orderyouhui"]').html('<li>无</li>');
							} else {
								var tpl = '';
								$.each(orderPmtList, function(index, item) {
									tpl += '<li><b>['+item.pmtTag+']</b>'+item.pmtDescribe+'</li>';
								})
								$('[data-role="orderyouhui"]').html(tpl);
							}


							// 订单备注
							$('[data-role="memo"]').html(orders.memo || '暂无附言');

							//金额
							$('[data-result="totalmoney"]').html('+¥'+orders.costItem);
							$('[data-result="yunfei"]').html('¥'+orders.costFreight);
							$('[data-result="savemoney"]').html('-¥'+orders.pmtOrder);
							$('[data-result="score"]').html('(订单可得积分'+orders.scoreG+')');
							$('[data-result="money"]').html('¥'+orders.finalAmount);


						} else {
							wsCommon.setCookie('go_back_link', window.location.href);
							window.location.href = './passport-login.html';
					   }
				   }
			    },
			    error: function() {
			        // console.log('订单详情接口返回失败');
			    }
			})
		},
		init: function() {
			this.getData();
		}
	}

	orderDetail.init();
})
